
import nuxtStorage from 'nuxt-storage'
const whiteList = ['/login']
export default ({ app, store, redirect }) => {
  app.router.beforeEach(async(to, from, next) => {
    const token = app.$cookies.get('token')
    store.commit("setToken", token)
    if (store.state.token) {
      if (to.path === '/login') {
        next()
      } else {
        const userInfo = store.getters['user/userInfo']
        if (Object.keys(userInfo).length) {
          next()
        } else {
          try {
            await store.dispatch('user/getUserInfo')
            next()
          } catch (error) {
            next(`/login?redirect=${to.path}`)
          }
        }
      }
    } else {
      if (whiteList.indexOf(to.path) !== -1) {
        next()
      } else {
        next(`/login?redirect=${to.path}`)
      }
    }
  })
};
